$(function () {
  $('#uploaderInput').on('change', function () {
    var files = $(this)[0].files
    var count = files.length
    var fileSize = 0
    var fd = new FormData()//创建表单数据对象
    for (var index = 0; index < count; index++) {
      var file = files[index]
      fd.append('file', file)
      if (file.size > 1024 * 1024)
        fileSize = (Math.round(file.size * 100 / (1024 * 1024)) / 100).toString() + 'MB'
      else
        fileSize = (Math.round(file.size * 100 / 1024) / 100).toString() + 'KB'
      upload(file)
    }
    console.log(fileSize)
    var count = files.length
    var xhr = new XMLHttpRequest()
    //xhr.upload.addEventListener("progress", funs.uploadProgress, false);//监听上传进度
    //xhr.addEventListener("load", funs.uploadComplete, false);

    xhr.addEventListener('error', function (ev) { console.error(ev) }, false)
    xhr.addEventListener('success', function (data) {console.log(xhr.responseText)})
    xhr.open('POST', '/s/wx/register/upload')
    xhr.send(fd)
    xhr.onreadystatechange = function () {
      //若响应完成且请求成功
      if (xhr.readyState === 4 && xhr.status === 200) {
        var data = JSON.parse(xhr.responseText)
        console.log(data.data.src)
        $('#zp').val(data.data.src)
      }
    }
  })

  function upload (file) {
    var gallery = $('.weui-uploader__input-box')
    var obj = $('.weui-uploader__input-box').find('img')
    if (obj != undefined && obj.length != 0) {
      obj.remove()
    }
    var img = document.createElement('img')
    img.file = file
    //$(".weui-uploader__input-box").append(img);

    $('.weui-uploader__input-box').append(img)
    // 使用FileReader方法显示图片内容
    var reader = new FileReader()
    reader.onload = (function (aImg) {
      return function (e) {
        aImg.src = e.target.result
      }
    })(img)
    reader.readAsDataURL(file)
  }

  //城市
  $('#syd_sf').on('change', function () {
    var val = $(this).val()
    if ('' != val && !undefined != val) {
      var url = '/s/wx/register/' + val + '/city?time=' + new Date()
      $.ajax({
        type: 'get',
        url: url,
        dataType: 'json',
        async: false,
        contentType: 'application/json;charset=utf-8',
        error: function (request) {
          console.log('Connection error')
        },
        success: function (data) {
          var dataList = data.dataList
          if (dataList.length > 0) {
            var html = '<option value=\'\'>--请选择--</option>'
            for (var i = 0; i < dataList.length; i++) {
              html = html + '<option value=\'' + dataList[i].id + '\'>' + dataList[i].name + '</option>'
            }
            console.log(html)
            $('#sydCity').html(html)
          }
        }
      })
    }
  })
  //县
  $('#sydCity').on('change', function () {
    var val = $(this).val()
    if ('' != val && !undefined != val) {
      var url = '/s/wx/register/' + val + '/section?time=' + new Date()
      $.ajax({
        type: 'get',
        url: url,
        dataType: 'json',
        async: false,
        contentType: 'application/json;charset=utf-8',
        error: function (request) {
          console.log('Connection error')
        },
        success: function (data) {
          var dataList = data.dataList
          if (dataList.length > 0) {
            var html = '<option value=\'\'>--请选择--</option>'
            for (var i = 0; i < dataList.length; i++) {
              html = html + '<option value=\'' + dataList[i].id + '\'>' + dataList[i].name + '</option>'
            }
            $('#sydSection').html(html)
          }
        }
      })
    }
  })
  //专业
  $('#select3').on('change', function () {
    var val = $(this).val()
    if ('' != val && !undefined != val) {
      var url = '/s/wx/register/' + val + '/zy?time=' + new Date()
      $.ajax({
        type: 'get',
        url: url,
        dataType: 'json',
        async: false,
        contentType: 'application/json;charset=utf-8',
        error: function (request) {
          console.log('Connection error')
        },
        success: function (data) {
          var dataList = data.dataList
          if (dataList.length > 0) {
            var html = '<option value=\'\'>--请选择--</option>'
            for (var i = 0; i < dataList.length; i++) {
              html = html + '<option value=\'' + dataList[i].id + '\'>' + dataList[i].zymc + '</option>'
            }
            $('#select4').html(html)
          }
        }
      })
    }
  })
  $('#showTooltips').on('click', function () {
    var info = true
    var msg = ''
    var xm = $('input[name=\'xm\']').val()
    if ('' == xm || undefined == xm) {
      info = false
      msg = '姓名不能为空！'
      alert(msg)
      return
    }
    var zp = $('#zp').val()
    if ('' == zp || undefined == zp) {
      info = false
      msg = '照片不能为空！'
      alert(msg)
      return
    }
    var syd_sf = $('#syd_sf').val()
    if ('' == syd_sf || undefined == syd_sf) {
      info = false
      msg = '所属省份不能为空！'
      alert(msg)
      return
    }
    var sydCity = $('#sydCity').val()
    if ('' == sydCity || undefined == sydCity) {
      info = false
      msg = '所属城市不能为空！'
      alert(msg)
      return
    }
    var sydSection = $('#sydSection').val()
    if ('' == sydSection || undefined == sydSection) {
      info = false
      msg = '所属区不能为空！'
      alert(msg)
      return
    }
    var jg = $('input[name=\'jg\']').val()
    if ('' == jg || undefined == jg) {
      info = false
      msg = '籍贯不能为空！'
      alert(msg)
      return
    }
    var gbtMzdm = $('#gbtMzdm').val()
    if (gbtMzdm == '' || undefined == gbtMzdm) {
      info = false
      msg = '民族不能为空'
      alert(msg)
      return
    }
    var sfzjh = $('input[name=\'sfzjh\']').val()
    if ('' == sfzjh || undefined == sfzjh) {
      info = false
      alert('身份证件号不能为空！')
      return
    }
    var select3 = $('#select3').val()
    if ('' == select3 || undefined == select3) {
      info = false
      alert('预报名系部不能为空！')
      return
    }
    var select4 = $('#select4').val()
    if ('' == select4 || undefined == select4) {
      info = false
      alert('预报名专业不能为空！')
      return
    }
    if (info) {
      $('#form').submit()
    } else {
      alert(msg)
    }

  })

    $('#btn').on('click', function () {

        var info = true
        var msg = ''
        var username = $('input[name=\'username\']').val()
        if ('' == username || undefined == username) {
            info = false
            msg = '姓名不能为空！'
            alert(msg)
            return
        }
        var password = $('input[name=\'password\']').val()
        if ('' == password || undefined == password) {
            info = false
            msg = '密码不能为空！'
            alert(msg)
            return
        }

        if (info) {
            $('#form').submit()
        } else {
            alert(msg)
        }

    })
})

    